<template>
	<div>
		<el-dialog :title="'排重校验中'" :visible.sync="checkLoading" width="20%">
			<div id="qrCode">
				<div id="code" ref="qrCodeUrl" align="center">正在进行排重校验，请稍后</div>
			</div>
		</el-dialog>
		<el-dialog title="重复试题" :visible.sync="visible" v-dialogDrag append-to-body :modal-append-to-body="false" top="5vh"
			width="50%">
			<div class="row-me margin-bottom20">
				<el-button :type="btnIndex==1?'primary':''" class="margin-right30" @click="clickBtn(1)">全部</el-button>
				<el-button :type="btnIndex==3?'primary':''" class="margin-right30" @click="clickBtn(3)">平台发布</el-button>
				<el-button :type="btnIndex==2?'primary':''" class="margin-right30" @click="clickBtn(2)">机构发布</el-button>
			</div>
			<el-card class="box-card font-size14" style="margin-bottom: 10px;" v-for="(question, index) in tableData"
				:key="index">
				<div style="display: flex;justify-content: space-between">
					<b>
						<p v-html="index + 1 + '.' + question.title"></p>
					</b>
					<el-button type="primary" @click="handleEdit(question)" class="height40">编辑</el-button>
				</div>
				<div v-if="question.type.value == 0 || question.type.value == 1">
					<p v-for="(item, index) in question.optionList">{{ optionNoArr[index] }}. {{ item.content }}</p>
				</div>
				<b>
					<p style="color:red">答案：<span v-html="question.answer"></span></p>
				</b>
				<b>
					<p>解析：<span v-html="question.textExplain"></span></p>
				</b>
				<div class="row-me row-center">
					<div>
						<el-tag type="warning">分类</el-tag>
						<span class="margin-left10">{{question.category3}}/{{question.category2}}/{{question.category1}}</span>
					</div>
					<div class="margin-left60">
						<el-tag type="warning">来源</el-tag><span class="margin-left10">{{getSource(question)}}</span>
					</div>
				</div>
			</el-card>
		</el-dialog>
		<el-dialog title="检测到重复试题列表" :visible.sync="editVisible" append-to-body :modal-append-to-body="false" top="5vh"
			width="70%" @close="clickBtn(btnIndex)">
			<div class="width100Per">
				<examQuestions ref="examQuestions" :questionId="questionId" :isSmall="isSmall" @repeatedOk="close">
				</examQuestions>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		name: 'repeatedQuestions',
		props: ['form', 'isSmall'],
		components: {
			examQuestions: () => import('@/components/examQuestions/index.vue')
		},
		data() {
			return {
				visible: false,
				editVisible: false,
				tableData: [],
				optionNoArr: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'], //选择题题号数组
				questionId: '',
				btnIndex: 1,
				checkLoading: false,
				batchImportInfo: {},
				page: ''
			}
		},
		methods: {
			getSource(item) {
				let arr = []
				if (item.examSource && item.examSource.length != 0) {
					arr = [...item.examSource]
				}
				if (item.questionSources && item.questionSources.length != 0) {
					arr = [...arr, ...Array.from(item.questionSources, ({
						name
					}) => name)]
				}
				return arr.toString()
			},
			close() {
				// this.visible = false
				this.editVisible = false
				this.checkLoading = false
				this.clickBtn(this.btnIndex)
			},
			clickBtn(index) {
				this.btnIndex = index
				if (this.page == 'batchImport') {
					this.getSimilarity2(this.batchImportInfo)
				} else {
					this.getSimilarity()
				}

			},
			show(page = '', row = {}) {
				if (page == 'batchImport') {
					this.page = 'batchImport'
					this.batchImportInfo = row
					this.tableData = row.respeatedList || []
					this.visible = true
				} else {
					this.checkLoading = true;
					this.getSimilarity()
				}

			},
			getSimilarity2(item) {
				let option = {
					isCorrect: false,
					optionNo: '',
					content: '',
					type: 'text'
				};
				option = JSON.stringify(item.optionList);
				option = encodeURIComponent(option);
				this.$get('/admin/question/similarity', {
					title: item.title,
					questionType: item.type,
					option,
					roleType: this.btnIndex == 2 ? 2 : '',
					isChild: false
				}).then(res => {
					if (res.code == 1000) {
						if (res.data.length != 0) {
							setTimeout(() => {
								this.tableData = res.data
							})
						}
					}
				}).catch(() => {

				})
			},
			getSimilarity() {
				var optionArr = this.form.optionsStr ? this.form.optionsStr.split('\n') : [];
				let optionList = [];
				let option = {
					isCorrect: false,
					optionNo: '',
					content: '',
					type: 'text'
				};
				for (let i = 0; i < optionArr.length; ++i) {
					//去除字符串.之前的ABCD和字符串后面的空格
					option.content = optionArr[i].substring(optionArr[i].indexOf('.') + 1, optionArr[i].length)
						.trimRight();
					optionList.push(Object.assign({}, option));
				}
				if (this.form.optionType == 2) {
					for (let index in this.imgOptionData) {
						this.imgOptionData[index].type = 2
						this.imgOptionData[index].title = this.optionsArr[index]
					}
					optionList = this.imgOptionData
				}
				option = JSON.stringify(optionList);
				option = encodeURIComponent(option);
				this.$get('/admin/question/similarity', {
					title: this.form.title,
					questionType: this.form.type,
					option,
					roleType: this.btnIndex == 2 ? 2 : '',
					isChild: this.isSmall
				}).then(res => {
					this.checkLoading = false
					if (res.code == 1000) {
						if (res.data.length != 0) {
							setTimeout(() => {
								this.tableData = res.data
							})
							this.visible = true
						} else {
							this.$message.success('没有检测到重复的试题')
							this.tableData = []
						}
					} else {
						this.$message.error('排重失败，请重试')
					}
				}).catch(() => {
					this.checkLoading = false
				})
			},
			handleEdit(question) {
				console.log(question, "question")
				setTimeout(() => {
					this.editVisible = true
					this.$refs.examQuestions.showDetail()
				})
				this.questionId = question.id
				this.isSmall = question.isChild
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>